package utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class FileManager {

	private List<String> filesDir;
	private File root = null;

	public FileManager() {
		filesDir = new LinkedList<String>();
	}

	public void writeLogger(String loggerPath, String content)
			throws IOException {
		File file = new File(loggerPath);
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(content);
		bw.close();
	}

	public void writeTransformation(List<String> contentTransformation,
			String transformationPath) throws IOException {
		File file = new File(transformationPath);
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		String content = "";
		Iterator<String> iterator = contentTransformation.iterator();
		while (iterator.hasNext()) {
			content += iterator.next() + Constant.ENDOFLINE;
		}
		bw.write(content);
		bw.close();

	}

	public List<String> readTransformation(File file) throws IOException {
		FileReader fr = new FileReader(file);
		BufferedReader bf = new BufferedReader(fr);
		ArrayList<String> contentTransformation = new ArrayList<String>();
		String lineCode = bf.readLine();
		while (lineCode != null) {
			contentTransformation.add(lineCode);
			lineCode = bf.readLine();
		}
		return contentTransformation;

	}

	private void getTransformationsPath(File dir) {
		File[] dirs = dir.listFiles();
		if (dirs.length == 0) {
			return;
		} else {
			for (File dirAux : dirs) {
				if (dirAux.getName().endsWith(".svn") || !dirAux.isDirectory()) {
					continue;
				}
				filesDir.add(dirAux.getAbsolutePath());
				getTransformationsPath(dirAux);
			}
		}

	}

	public void createDir(String path) {
		File file = new File(path);
		file.mkdirs();
	}

	public File getLoggerPath(String path, String extension) {
		File dir = new File(path);
		// FilenameFilter selectLogger = new FileListFilter("", extension);
		return dir;

	}

	public List<String> getTransformations(String path, String extension)
			throws IOException {
		filesDir.clear();
		File dirs = new File(path);
		this.root = dirs;
		this.filesDir.add(dirs.getAbsolutePath());
		getTransformationsPath(dirs);
		FilenameFilter selectM2T = new FileListFilter("", extension);
		List<String> transformationsPath = new LinkedList<String>();
		for (String dir : filesDir) {
			createDir(dir.replace(this.root.getName(), this.root.getName()
					+ Constant.INSTRUMENTED));
			File auxDir = new File(dir);
			File[] transformations = auxDir.listFiles(selectM2T);
			for (File transformation : transformations) {
				transformationsPath.add(transformation.getAbsolutePath());
			}
		}
		return transformationsPath;

	}

	public File getCurrentRoot() {
		return this.root;
	}

	class FileListFilter implements FilenameFilter {
		private String name;

		private String extension;

		public FileListFilter(String name, String extension) {
			this.name = name;
			this.extension = extension;
		}

		public boolean accept(File directory, String filename) {
			boolean fileOK = true;

			if (name != null) {
				fileOK &= filename.startsWith(name);
			}

			if (extension != null) {
				fileOK &= filename.endsWith('.' + extension);
			}
			return fileOK;
		}
	}

}
